package com.zhuss.core.processor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.user.CreateUserDeptBo;
import com.zhuss.bean.bo.core.user.DeleteUserDeptBo;
import com.zhuss.bean.bo.core.user.UserDeptBindBo;
import com.zhuss.bean.vo.core.user.DetailUserVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.UserDao;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.UserDeptDeleteType;
import com.zhuss.exception.AabException;
import com.zhuss.util.JsonUtil;

/**
 * 给用户绑定部门 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/11 11:50
 */
@Component("userDeptBind")
public class UserDeptBind extends AbstractProcessor<Object, UserDeptBindBo> implements Processor<Object, UserDeptBindBo> {

    @Autowired
    private UserDao userDao;
    
    @Autowired
	@Qualifier("userRead")
	private UserRead userRead;
    
    @Autowired
	@Qualifier("userDeptRead")
	private UserDeptRead userDeptRead;
    
    @Autowired
	@Qualifier("userDeptCreate")
	private Processor<Object, CreateUserDeptBo> userDeptCreate; 
    
    @Autowired
	@Qualifier("userDeptDelete")
	private Processor<Object, DeleteUserDeptBo> userDeptDelete;
    
    
    /**
     * 校验
     */
    @Override
    public void check(UserDeptBindBo bo) throws AabException {
    	
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(UserDeptBindBo bo) throws AabException {
    	
    	Long userId = bo.getUserId();
    	List<Long> paramList = bo.getDeptIdList();
    	@SuppressWarnings("unchecked")
		List<Long> dbList = (List<Long>)userDeptRead.listDeptId(userId);
    	List<Long> addList = new ArrayList<Long>(paramList);
    	List<Long> removeList = new ArrayList<Long>(dbList);
    	
    	removeList.removeAll(paramList);
    	addList.removeAll(dbList);
    	
    	if (!removeList.isEmpty()) {
    		DeleteUserDeptBo param = new DeleteUserDeptBo();
    		param.setType(UserDeptDeleteType.根据用户删除用户部门关系.key());
    		param.setUserId(userId);
    		param.setDeptIdList(removeList);
    		
    		param.setModifierId(bo.getConcurrentUserId());
    		param.setModifier(bo.getConcurrentUserName());
    		param.setModifyTime(new Date());
    		userDeptDelete.process(param);
    	}
    	
    	for (Long deptId : addList) {
    		CreateUserDeptBo param = new CreateUserDeptBo();
    		BeanUtils.copyProperties(bo, param);
    		param.setUserId(userId);
    		param.setDeptId(deptId);
    		userDeptCreate.process(param);
    	}
    	
    	String name = ((DetailUserVo) userRead.detail(userId)).getName();
    	bo.setName(name);
    	bo.setAddList(addList);
    	bo.setRemoveList(removeList);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(UserDeptBindBo param) throws AabException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.用户部门绑定.key());
    	record.setTargetId(param.getUserId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
		return null;
	}
}
